package ru.rchervot.sanasar.server;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gwt.libideas.server.LocaleMatcher;

public class StartPage extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException
	{
		

		// Setup code
//	  Resource resource = Resource.newResource("my_deployed_app_path/localeList/locale-list.txt");
//	  LocaleMatcher localeMatcher = new LocaleMatcher(resource.getInputStream());
		
		// instead 
		
		String localeList = "default\nru\n";
		LocaleMatcher localeMatcher = new LocaleMatcher(convertStringtoStream(localeList));
	  String locale = localeMatcher.findBestMatch(request.getHeader("Accept-language"));
	  
	  // Store the selected locale in the host html page to be served, via
	  // <meta name='gwt:property' content='locale=<<locale>>'>

	  response.setContentType("text/html; charset=UTF-8");
	  // Be sure to include a Vary: Accept-language header in the response.
	  response.addHeader("Vary", "Accept-language");
	  
		PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
		
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
		out.println("<meta name=\"gwt:property\" content=\"locale=" + locale + "\">");
		out.println("<!-- this css should be here not in module xml because collapsible panel would not work   -->");
		out.println("<link type=\"text/css\" rel=\"stylesheet\" href=\"Collapsible.css\">");
		out.println("<title>Sanasar</title>");
		out.println("<script type=\"text/javascript\" language=\"javascript\" src=\"sanasar/sanasar.nocache.js\"></script>");
		out.println("</head>");
		out.println("<body>");
		out.println("<iframe src=\"javascript:''\" id=\"__gwt_historyFrame\" tabIndex='-1' style=\"position:absolute;width:0;height:0;border:0\"></iframe>");
		out.println("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tbody>");
		out.println("<tr><td id=\"Header\"></td></tr>");
		out.println("<tr><td id=\"ModuleCandidates\"></td></tr>");
		out.println("<tr><td id=\"ModuleCompanies\"></td></tr>");
		out.println("<tr><td id=\"ModuleOrders\"></td></tr>");
		out.println("<tr><td id=\"ModuleReports\"></td></tr>");
		out.println("<tr><td id=\"ModuleSettings\"></td></tr>");
		out.println("</tbody></table>");
		out.println("</body>");
		out.println("</html>");
	}
	
	private InputStream convertStringtoStream(String s) {
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(s.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return is;
	}
}